//
//  VerCouponV.m
//  ScalperMerchant
//
//  Created by 王云 on 2017/10/7.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "VerCouponV.h"

typedef void(^option)(NSDictionary *fieldDic);
typedef void(^delClick)(NSString *index);
@interface VerBtnV : UIView

@property (nonatomic,copy)option click;
@property (nonatomic,copy)delClick dClcik;

@property (nonatomic,weak)UITextField *inputL;
@property (nonatomic,weak)UIButton *delBtn;

@property (nonatomic,copy)NSString *index;

@property (nonatomic,strong)NSDictionary *dic;

@property (nonatomic,assign)NSInteger hidden;

@end

@implementation VerBtnV

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    
    UIImageView *iconImg = [[UIImageView alloc] init];
    iconImg.image = [UIImage imageNamed:@"quan"];
    [self addSubview:iconImg];
    
    UILabel *couponL = [[UILabel alloc] init];
    couponL.text = @"劵码";
    couponL.font = [UIFont systemFontOfSize:12];
    [self addSubview:couponL];
    
    UIImageView *vline = [[UIImageView alloc] init];
    vline.backgroundColor = DarkColor;
    [self addSubview:vline];
    
    UITextField *inputL = [[UITextField alloc] init];
    inputL.textColor = TitleDarkColor;
    [inputL addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventEditingChanged];
    inputL.placeholder = @"如 : 35313215123213";
    inputL.font = [UIFont systemFontOfSize:12];
    [self addSubview:inputL];
    _inputL = inputL;
    
    UIImageView *hline = [[UIImageView alloc] init];
    hline.backgroundColor = DarkColor;
    [self addSubview:hline];
    
    UIButton *delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [delBtn setTitle:@"删除" forState:UIControlStateNormal];
    [delBtn setBackgroundColor:WYColor(254, 82, 56)];
    [delBtn addTarget:self action:@selector(delClick) forControlEvents:UIControlEventTouchUpInside];
    delBtn.layer.cornerRadius = 3;
    delBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [delBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self addSubview:delBtn];
    _delBtn = delBtn;
    
    __weak __typeof(&*self)weakSelf = self;
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf);
        make.left.equalTo(weakSelf.mas_left).offset(12);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    [couponL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf);
        make.left.equalTo(iconImg.mas_right).offset(8);
        make.width.equalTo(@40);
    }];
    [vline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf);
        make.left.equalTo(couponL.mas_right).offset(6);
        make.size.mas_equalTo(CGSizeMake(1, 20));
    }];
    [delBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf);
        make.right.equalTo(weakSelf.mas_right).offset(-12);
        make.size.mas_equalTo(CGSizeMake(50, 25));
    }];
    [inputL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(vline.mas_right).offset(8);
        make.right.equalTo(delBtn.mas_left).offset(-8);
        make.centerY.mas_equalTo(weakSelf);
    }];
    [hline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(weakSelf);
        make.height.mas_equalTo(@1);
    }];
}
- (void)setDic:(NSDictionary *)dic{
    _dic = dic;
    _index = _dic[@"index"];
    _inputL.text = _dic[@"coupon"];
}
- (void)editClick:(UITextField *)field{
    if (_click) {
        _click(@{@"index":_index,@"coupon":field.text});
    }
}
- (void)setHidden:(NSInteger)hidden{
    _hidden = hidden;
    _delBtn.hidden = _hidden;
}
- (void)delClick{
    if (_dClcik) {
        _dClcik(_index);
    }
}

@end

@implementation VerCouponV

- (instancetype)initWithFrame:(CGRect)frame data:(NSArray *)data{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.data = data;
    }
    return self;

}

- (void)setData:(NSArray *)data{
    _data = data;
    
    for (id obj in self.subviews) {
        if ([obj isKindOfClass:[VerBtnV class]]) {
            VerBtnV *btn = (VerBtnV *)obj;
            [btn removeFromSuperview];
        }
    }
        for (int i = 0; i < data.count; i++) {
        VerBtnV *btnV = [[VerBtnV alloc] initWithFrame:CGRectMake(0, 0, kWidth, 40)];
        btnV.dic = _data[i];
        if (i == 0) {
            btnV.hidden = YES;
        }else{
            btnV.hidden = NO;
        }
        btnV.click = ^(NSDictionary *fieldDic) {
            if (_Vclick) {
                _Vclick(fieldDic);
            }
        };
        btnV.dClcik = ^(NSString *index) {
            if (_DVclick) {
                _DVclick(index);
            }
        };
        [self addSubview:btnV];
        
        __weak __typeof(&*self)weakSelf = self;
        [btnV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf);
            make.top.equalTo(weakSelf.mas_top).offset(i*40);
            make.size.mas_equalTo(btnV.frame.size);
        }];
    }
}

@end
